import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

const Home = () =>
    import ('../views/home/Home')
const Cart = () =>
    import ('../views/cart/Cart')
const Mine = () =>
    import ('../views/mine/Mine')
const CateGory = () =>
    import ('../views/category/CateGory')
const Search = () =>
    import ('../views/search/Search')
const Detail = () =>
    import ('../views/detail/Detail')
const Login = () =>
    import ('../views/login/Login')
const Register = () =>
    import ('../views/register/Register')
const Collect = () =>
    import ('../views/mine/collect/Collect')
const OrderForm = () =>
    import ('../views/mine/orderform/OrderForm')
const MyOrder = () =>
    import ('../views/mine/orderform/MyOrder')
const MyQuotation = () =>
    import ('../views/mine/myquotation/MyQuotation')
const Quotation = () =>
    import ('../views/mine/myquotation/Quotation')
const MyInvoice = () =>
    import ('../views/mine/myinvoice/MyInvoice')
const MyAddress = () =>
    import ('../views/mine/myaddress/MyAddress')
const Payment = () =>
    import ('../views/mine/payment/Payment')
const Pim = () =>
    import ('../views/pim/Pim')
const PimRecord = () =>
    import ('../views/pim/component/PimRecord')
const PimRecord2 = () =>
    import ('../views/pim/component/PimRecord2')
const PimRecord3 = () =>
    import ('../views/pim/component/PimRecord3')
const PimRecord4 = () =>
    import ('../views/pim/component/PimRecord4')

Vue.use(VueRouter)


const routes = [

    {
        path: '/',
        name: 'home',
        component: Home,
        meta: {

        },
    },
    {
        path: '/category/',
        name: 'category',
        component: CateGory,

    },
    {
        path: '/search',
        name: 'search',
        component: Search,

    },
    {
        path: '/cart',
        name: 'cart',
        component: Cart,
        meta: {
            requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },

    },
    {
        path: '/mine',
        name: 'mine',
        component: Mine,
        meta: {
            requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },

    },
    {
        path: '/detail',
        name: 'detail',
        component: Detail,
        meta: {
            // requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },

    },
    {
        path: '/login',
        name: 'login',
        component: Login,


    }, {
        path: '/register',
        name: 'register',
        component: Register,

    }, {
        path: '/pim',
        name: 'pim',
        component: Pim,
        meta: {
            // requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },
        redirect: '/pim/pimrecord',
        children: [{
                path: 'pimrecord',
                name: 'PimRecord',
                component: PimRecord,
                meta: {
                    // requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
                },
            },
            {
                path: 'pimrecord2',
                name: 'PimRecord2',
                component: PimRecord2,
                meta: {
                    // requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
                },
            },
            {
                path: 'pimrecord3',
                name: 'PimRecord3',
                component: PimRecord3,
                meta: {
                    // requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
                },
            },
            {
                path: 'pimrecord4',
                name: 'PimRecord4',
                component: PimRecord4,
                meta: {
                    // requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
                },
            }
        ]
    }, {
        path: '/collect',
        name: 'collect',
        component: Collect,
        meta: {
            // requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },
    },
    {
        path: '/orderform',
        name: 'orderform',
        component: OrderForm,
        meta: {
            // requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },
    },
    {
        path: '/myorder',
        name: 'myorderform',
        component: MyOrder,
        meta: {
            // requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },
    },
    {
        path: '/myquotation',
        name: 'myquotation',
        component: MyQuotation,
        meta: {
            // requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },
    },
    {
        path: '/quotation',
        name: 'quotation',
        component: Quotation,
        meta: {
            // requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },
    },
    {
        path: '/myinvoice',
        name: 'myinvoice',
        component: MyInvoice,
        meta: {
            // requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },
    },
    {
        path: '/myaddress',
        name: 'myaddress',
        component: MyAddress,
        meta: {
            // requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },
    },
    {
        path: '/payment',
        name: 'payment',
        component: Payment,
        meta: {
            // requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },
    },
]

// 页面刷新时，重新赋值token
// if (localStorage.getItem('token')) {
//     store.commit("set_token", localStorage.getItem('token'))
// }


const router = new VueRouter({
    // mode: 'history',
    // base: process.env.BASE_URL,
    routes,
    store,
})

router.beforeEach((to, from, next) => {
    const token = localStorage.getItem("token")
    if (to.matched.some(route => route.meta.requireAuth)) {
        if (token) { //判断用户是否登录
            next()
        } else {
            next({
                path: "/login",
                query: { redirect: to.fullPath } //从哪来回哪去
            })
        }
    } else {
        next()
    }

})
export default router